import type {RouteRecordRaw} from "vue-router";
import router from "../router/index";

type IRoute = RouteRecordRaw & {id?:string};

// 定义全部路由：Admin下的子一级。
const allRoutes:IRoute[] = [
    // {
    //     id:"1",
    //     name:"/",
    //     path: "",
    //     redirect: "Admin/Home"
    // },
    // {
    //     id:"1",
    //     name:"Home",
    //     path: "Home",
    //     component: () => import("../views/Admin/Home/Home.vue")
    // },
    {
        id:"2-1",
        name:"CommodityList",
        path: "CommodityList",
        component: () => import("../views/Admin/Commodity/Commoditylist.vue")
    },
    {
        id:"2-2",
        name:"CommodityPage",
        path: "CommodityPage",
        component: () => import("../views/Admin/Commodity/Commoditypage.vue")
    },
    {
        id:"3-1",
        name:"BannerList",
        path: "BannerList",
        component: () => import("../views/Admin/Banner/List.vue")
    },
    {
        id:"3-2",
        name:"BannerAdd",
        path: "BannerAdd",
        component: () => import("../views/Admin/Banner/Add.vue")
    },
    {
        id:"4-1",
        name:"OperatorList",
        path: "OperatorList",
        component: () => import("../views/Admin/Operator/List.vue")
    },
    {
        id:"4-2",
        name:"OperatorAdd",
        path: "OperatorAdd",
        component: () => import("../views/Admin/Operator/Add.vue")
    },
   
    {
        id:"5-1",
        name:"Adress",
        path: "Adress",
        component: () => import("../views/Admin/Adress/Adress.vue")
    },
    {
        id:"6-1",
        name:"AllStatistic",
        path: "AllStatistic",
        component: () => import("../views/Admin/Statistic/AllStatistic.vue")
    },
    {
        id:"6-2",
        name:"Alluser",
        path: "Alluser",
        component: () => import("../views/Admin/Statistic/UserStatistic.vue")
    },
    {
        id:"7-1",
        name:"Echartsone",
        path: "Echartsone",
        component: () => import("../views/Admin/Echarts/Echartsone.vue")
    },
    {
        id:"7-2",
        name:"Echartstwo",
        path: "Echartstwo",
        component: () => import("../views/Admin/Echarts/Echartstwo.vue")
    }
]      

export default function getRoutes(){
    // 1、获取当前用户的权限
    let checkedKeys:string[] = [];
    const grants =  sessionStorage.getItem("grants");
    if(grants){
        checkedKeys = JSON.parse(grants);
    }
    
    if(checkedKeys.length===0){
        checkedKeys = ['1','2','2-1','2-2','3','3-1','3-2','4','4-1','4-2','5','5-1','6','6-1','6-2','7','7-1','7-2'];
    }

    // 2、获取当前用户的路由配置

    allRoutes.forEach(item=>{
        if(checkedKeys.includes(item.id as string)){
            // 动态添加路由配置： vueRouter对象.addRoute(父级路由的name，路由配置);
            router.addRoute("Admin",{...item});
        }
    })


    console.log("router.getRoutes()",router.getRoutes());
}

export const  clearCurrentRoutes = ()=>{
   // 1、获取当前用户的权限
   let checkedKeys:string[] = [];
   const grants =  sessionStorage.getItem("grants");
   if(grants){
       checkedKeys = JSON.parse(grants);
   }
   if(checkedKeys.length===0){
       checkedKeys = ['1','2','2-1','2-2','3','3-1','3-2','4','4-1','4-2','5','5-1','6','6-1','6-2','7','7-1','7-2'];
   }

   // 2、删除当前用户的路由配置

   allRoutes.forEach(item=>{
       if(checkedKeys.includes(item.id as string)){
           // 动态添加路由配置： vueRouter对象.addRoute(父级路由的name，路由配置);
        //    router.addRoute("Admin",{...item});
          router.removeRoute(item.name as string);
       }
   })

}